package listtest;

import java.util.Arrays;
import java.util.EmptyStackException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-03-26
 * Time: 20:56
 */
public class MyArrayList implements Ilist {
    public int[] arr;
    public int useSize;

    public MyArrayList() {
        this.arr = new int[10];

    }

    public boolean isFull() {
        return this.useSize == arr.length;
    }

    public void grow() {
        this.arr = Arrays.copyOf(this.arr, 2 * this.arr.length);
    }

    private void checkPos(int pos) throws PosIllegal{
        if (pos < 0 || pos > useSize) {
            throw new PosIllegal("pos位置不和法");
        }
    }
    private void checkPos2(int pos) throws PosIllegal{
        if (pos < 0 || pos >= useSize) {
            throw new PosIllegal("pos位置不和法");
        }
    }
    private void checkEmpty(){
        if(useSize == 0){
            throw new PosIllegal("顺序表为空")
        }
    }
    @Override
    public void add(int date) {
        if (isFull()) {
            grow();
        }
        this.arr[this.useSize] = date;
        this.useSize++;
    }

    @Override
    public void add(int pos, int date) {
        try {
            checkPos(pos);
            if (isFull()) {
                grow();
            }
            for (int i = useSize - 1; i >= pos; i--) {
                arr[i + 1] = arr[i];
            }
            arr[pos] = date;
            useSize++;
        }catch (PosIllegal e){
            System.out.println("插入的pos不合法");
            e.printStackTrace();
        }

    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(arr[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(arr[i] == toFind){
                return arr[i];
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        try {
            checkPos2(pos);
            checkEmpty();
            return arr[pos];
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyStackException e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try {
            checkPos2(pos);
            checkEmpty();
            arr[pos] = value;
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyStackException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
            int pos = indexOf(toRemove);
            if(pos == -1){
                return;
            }
            for (int i = pos; i < useSize - 1; i++) {
                arr[i] = arr[i + 1];
            }
        }catch (EmptyStackException e){
            e.printStackTrace();
        }

    }

    @Override
    public int size() {
        return useSize;
    }

    @Override
    public void clear() {
        /*for (int i = 0; i < useSize; i++) {
            arr[i] = null;
        }*/
        useSize = 0;

    }
    @Override
    public void display() {
        for (int i = 0; i < this.useSize; i++) {
            System.out.print(arr[i] + " ");
        }
    }

}
